package com.dries.portal.module.portal.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dries.common.api.CommonResult;
import com.dries.portal.module.portal.entity.ShopCollect;
import com.dries.portal.module.portal.service.IShopCollectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 店铺收藏
 *
 * @author dries
 * @email samphsanie@gmail.com
 * @date 2020-08-27 11:08:38
 */
@RestController
@Api(tags = "店铺收藏API", value = "ShopCollectController")
@RequestMapping("shop/collect")
public class ShopCollectController {

    @Resource
    private IShopCollectService shopCollectService;

    /**
     * 用户收藏列表
     */
    @ApiOperation("用户收藏列表")
    @GetMapping("/list/{userId}")
    public CommonResult list(@PathVariable String userId){
        List<ShopCollect> list = shopCollectService.list(new QueryWrapper<ShopCollect>().lambda()
                .eq(ShopCollect::getUserId, userId)
        );
        return CommonResult.success(list);
    }

    /**
     * 是否收藏
     */
    @ApiOperation("是否收藏")
    @GetMapping("/check/{userId}/{shopId}")
    public CommonResult isCollect(@PathVariable String userId, @PathVariable String shopId) {

        ShopCollect collect = shopCollectService.getOne(new QueryWrapper<ShopCollect>().lambda()
                .eq(ShopCollect::getUserId, userId)
                .eq(ShopCollect::getShopId, shopId)
        );
        if (collect != null) {
            return CommonResult.success(1);
        } else {
            return CommonResult.success(0);
        }
    }

    /**
     * 收藏店铺
     */
    @ApiOperation("收藏店铺")
    @PostMapping("/save")
    public CommonResult save(@RequestBody ShopCollect shopCollect){
        ShopCollect collect = shopCollectService.getOne(new QueryWrapper<ShopCollect>().lambda()
                .eq(ShopCollect::getUserId, shopCollect.getUserId())
                .eq(ShopCollect::getShopId, shopCollect.getShopId())
        );
        if (collect != null) {
            return CommonResult.success();
        }
        return CommonResult.success(shopCollectService.save(shopCollect));
    }

    /**
     * 取消收藏
     */
    @ApiOperation("取消收藏")
    @DeleteMapping("/delete")
    public CommonResult delete(@RequestBody Integer[] ids){
        return CommonResult.success(shopCollectService.removeByIds(Arrays.asList(ids)));
    }

}
